var batchNodeController = new function(){
	
	var $kendoWindow
	  , $openWindowBtn
	  , $title
	  , $list
	  , $filed
	  , $objectBatchNode
	  , _selectDataObject
	;
	
	var BATCH_NODE_TMPL = '<li><span></span><a class="remove-batch-btn"></a></li>';
	
	/*
	 *  batch node object for each scene
	 */
	function batchNode(){
		
		var elems = [];
		
		function remove(ele){
			
			$.each(elems, function(i,e){
				if (e.name == ele){
					elems.splice(i,1)
				}
			});
			
			return elems;
		}
		
		function add(ele){
			
			var found = false;
			$.each(elems, function(i,e){
				if (e.name == ele){
					found = true;
				}
			});
			
			if (found){
				window.console.log('El batch node ' + ele + ' ya está incluído en esta escena.')
				return false;
			}
			else {
				elems.push({"name": ele})
				return true;
			}
		}
		
		function get(){
			return elems;
		}
		
		function getAsArray(){
			
			var r = [];
			$.each(elems, function(i,e){
				
				r.push(e.name)
			})
			return r;
		}
		
		function set(elems){
			
			if (elems){
				
				for (var i=0;i<elems.length;i++){
					add(elems[i]);
				}
			}
		}
		
		this.remove = remove;
		this.add = add;
		this.get = get;
		this.getAsArray = getAsArray;
		this.set = set;
		
	}//end batchNode object
	
	
	/*
	 *  inflate batch node list in batch node window
	 */
	function inflate(scene){	
		
		var sceneId = scene.id;
		var sceneName = scene.item.attr('scenename');
		var batchNodes = scene.batchNodes.get();
		
		$title.html('Batch nodes de la escena <i>' + sceneName + '</i>');
		$list.empty();
		
		
		$.each(batchNodes, function(i,e){
			
			createListElem(e.name);
		});
		
	}//end inflate function
	
	
	function createListElem(e){
		
		var elem = $(BATCH_NODE_TMPL);
		elem.find('span').text(e);
		elem.appendTo( $list );
	}
	
	
	function createBatchNode4Scene(){
		
		return new batchNode();
	}
	
	
	function onUploadFile(data){

		var fileName = null;
		
		try {
			fileName = data.files[0].name;
		}
		catch(e){
			window.console.error('Error en la captura del fichero.', e)
		}
		
		if (!fileName){
			
			window.console.error('Error en la captura del fichero.')
			return;
		}
		
		if (fileName.lastIndexOf('.') != -1) {
			fileName = fileName.substring(0, fileName.lastIndexOf('.'));
		}
		
		
		var scene = sceneList.selected();
		if (scene){
			
			if (scene.batchNodes.add(fileName)){
				createListElem(fileName);
				updateSelector();
			}
		}
	}
	
	
	function init(){
		
		if (!_selectDataObject){
			_selectDataObject = new kendo.data.DataSource();	
		}
		
		$kendoWindow = $('#batch-node-window').kendoWindow({
			  width: "700px"
			, height: "350px"
			, title: ""
			, modal: true
			, visible: false
			, draggable: false
			, resizable: false
		});
		
		$title = $('#batch-node-window-title');
		$list = $('#batch-node-window-list');
		$file = $('#batch-node-window-select');
		
		
		$("#batch-node-window-select").kendoUpload({
        	async: {
                saveUrl: "/s/batchNode",
                autoUpload: true
            },
            multiple: false,
            showFileList: false,
            success: onUploadFile,
            error: onUploadFile,
            localization: {
            	select: "Añadir"
            }
        });
		
		$openWindowBtn = $('#batch-nodes-img');
		
		
		$openWindowBtn.click(function(){
			
			var scene = sceneList.selected();
			if (scene){

				inflate(scene);
				$kendoWindow.data("kendoWindow").center().open();	
			}
			else {
				window.console.log('No hay ninguna escena seleccionada.')
			}
		})
		
		$objectBatchNode = $('#batch-node-input')
			.kendoComboBox({
				dataTextField: "name",
			    dataValueField: "name",
			    dataSource: _selectDataObject,
			    readOnly: true,
			    enable: false,
			    placeholder: "Seleccionar",
			    change: function(e){
			    	
			    	if (leftTree.selected()){
			    		leftTree.selected().batchNode = $objectBatchNode.value();
			    	}
			    }
			}).data("kendoComboBox");
	
	}//end init function
	
	function updateSelector(){
		
		var elems = sceneList.selected().batchNodes.get();
		if (!_selectDataObject){
			_selectDataObject = new kendo.data.DataSource();
		}
		_selectDataObject.data(elems);
	
		if ($objectBatchNode && leftTree.selected()){
			$objectBatchNode.select(function(dataItem) {
				
			    return dataItem.name === leftTree.selected().batchNode
			});
		} 
	}
	
	function select(p_enable){
		
		var enable = (typeof p_enable === "boolean") ? p_enable : true;
		
		$objectBatchNode.enable(enable);
		
		if (enable){
			if ($objectBatchNode && leftTree.selected()){
				$objectBatchNode.value(leftTree.selected().batchNode)
			} 
		}
		else {
			$objectBatchNode.value('')
		}
	}
	
	this.createBatchNode4Scene = createBatchNode4Scene;
	this.init = init;
	this.updateSelector = updateSelector;
	this.select = select;
	
	
}